package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PictureDao;
import com.itheima.dao.PictureSortDao;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.Picture;
import com.itheima.pojo.PictureSort;
import com.itheima.service.PictureService;
import com.itheima.utils.*;
import com.itheima.vo.PictureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class PictureServiceImpl implements PictureService {
    @Autowired
    private PictureDao pictureDao;
    @Autowired
    private PictureSortDao pictureSortDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 添加图片
     *
     * @param pictureVOList
     * @return
     */
    @Override
    public String addPicture(List<PictureVO> pictureVOList) {
        if (pictureVOList.size() > 0) {
            for (PictureVO pictureVO : pictureVOList) {
                Picture picture = new Picture();
                picture.setUid(idWorker.nextId()+"");
                picture.setFileUid(pictureVO.getFileUid());
                picture.setPictureSortUid(pictureVO.getPictureSortUid());
                picture.setPicName(pictureVO.getPicName());
                picture.setStatus(EStatus.ENABLE);
                pictureDao.insert(picture);
            }
        } else {
            return ResultUtil.result(StatusCode.ERROR, "添加失败");
        }
        return ResultUtil.result("success", "添加成功");
    }

    /**
     * 删除图片
     *
     * @param pictureVO
     * @return
     */
    @Override
    public String deletePicture(PictureVO pictureVO) {
        String uidStr = pictureVO.getUid();
        if (StringUtils.isEmpty(uidStr)) {
            return ResultUtil.result(StatusCode.ERROR, "删除失败");
        }
        List<String> uids = StringUtils.changeStringToString(pictureVO.getUid(), ",");
        for (String item : uids) {
            Picture picture = pictureDao.selectByPrimaryKey(item);
            //删除做逻辑删除
            picture.setStatus(EStatus.DISABLED);
            picture.setUpdateTime(DateUtils.getNowTime());
            pictureDao.updateByPrimaryKey(picture);
        }
        return ResultUtil.result("success", "删除成功");
    }

    /**
     * 编辑图片
     *
     * @param pictureVO
     * @return
     */
    @Override
    public String editPicture(PictureVO pictureVO) {
        Picture picture = new Picture();
        picture.setUid(pictureVO.getUid());
        picture.setFileUid(pictureVO.getFileUid());
        picture.setPicName(pictureVO.getPicName());
        picture.setPictureSortUid(pictureVO.getPictureSortUid());
        picture.setStatus(pictureVO.getStatus());
        picture.setUpdateTime(DateUtils.getNowTime());
        pictureDao.updateByPrimaryKeySelective(picture);
        return ResultUtil.result("success", "编辑成功");
    }

    /**
     * 获取图片列表
     *
     * @param pictureVO
     * @return
     */
    @Override
    public Page<Picture> getPageList(PictureVO pictureVO) {
        //设置默认当前页
        if (pictureVO.getCurrentPage() == null || "".equals(pictureVO.getCurrentPage())) {
            pictureVO.setCurrentPage(0);
        }
        //设置默认每页条数
        if (pictureVO.getPageSize() == null || "".equals(pictureVO.getPageSize())) {
            pictureVO.setPageSize(10);
        }
        //当前页
        Integer currentPage = pictureVO.getCurrentPage();
        //每页数据
        Integer pageSize = pictureVO.getPageSize();
        //分页查询
        PageHelper.startPage(currentPage, pageSize);
        //查询条件构造
        Example example = new Example(Picture.class);
        Example.Criteria criteria = example.createCriteria();
        //状态为1的才可以被查询到
        criteria.andEqualTo("status", 1);
        return (Page<Picture>) pictureDao.selectByExample(example);
    }

    /**
     * 通过Uid设置为封面
     *
     * @param pictureVO
     * @return
     */
    @Override
    public String setPictureCover(PictureVO pictureVO) {
        //封面是分类封面
        //就需要设置图片分类的文件id就可以把图片展示到分类里面
        //先根据pictureSortUid查询是否有对应的图片分类信息
        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(pictureVO.getPictureSortUid());

        pictureSort = pictureSortDao.selectOne(pictureSort);

        if (pictureSort == null) {
            throw new RuntimeException("没有对应图片分类UID");
        }

        //有就将图片的id设置为图片分类的fileUid
        pictureSort.setFileUid(pictureVO.getUid());
        pictureSort.setUpdateTime(DateUtils.getNowTime());

        pictureSortDao.updateByPrimaryKeySelective(pictureSort);
        return ResultUtil.result("success", "设置成功成功");
    }
}
